<template>
    <div>
        <el-main>

            <el-breadcrumb separator="/" style="display: inline-block;margin-left: 10px">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>角色管理</el-breadcrumb-item>
            </el-breadcrumb>

            <div style="padding: 10px 0">
                <el-input style="width: 200px;margin-left: 5px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"> </el-input>
                <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>
                <el-button style="margin-left: 5px" type="warning" @click="reset">重置</el-button>
            </div>
            <div style="margin: 10px 0">
                <el-button type="primary" @click="newadd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
                <el-popconfirm
                        confirm-button-text='确定'
                        cancel-button-text='我在想想'
                        icon="el-icon-info"
                        icon-color="red"
                        title="这是一段内容确定删除吗？"
                        @confirm="alldelete"
                >
                    <el-button type="danger"  slot="reference" style="margin: 0px 15px">批量删除 <i class="el-icon-remove-outline"></i></el-button>
                </el-popconfirm>

            </div>

            <el-table :data="tableData" border stripe  @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column prop="id" label="ID" width="80"></el-table-column>
                <el-table-column prop="name" label="名称"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column label="操作"  align="center" width="300">
                    <template slot-scope="scope">
                        <el-button type="info" @click="selectMenu(scope.row)">分配菜单 <i class="el-icon-menu"></i></el-button>
                        <el-button type="success" @click="update(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
                        <el-popconfirm
                                confirm-button-text='确定'
                                cancel-button-text='我在想想'
                                icon="el-icon-info"
                                icon-color="red"
                                title="这是一段内容确定删除吗？"
                                @confirm="remove(scope.row.id)"
                        >
                            <el-button type="danger" slot="reference"  style="margin-left: 15px">删除 <i class="el-icon-remove-outline"></i></el-button>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>
            <div style="padding: 10px 0">
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pageNum"
                        :page-sizes="[2, 5, 10]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>

                <el-dialog :title="menutitle" :visible.sync="MenudialogFormVisible" width="30%">
                    <el-tree
                            :data="menuData"
                            show-checkbox
                            node-key="id"
                            :default-expanded-keys="[1]"
                            :default-checked-keys="[4]"
                            @check-change="handleCheckChange">
                    </el-tree>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="MenudialogFormVisible= false">取 消</el-button>
                        <el-button type="primary" @click="Add">确 定</el-button>
                    </div>
                </el-dialog>

                <el-dialog :title="menutitle" :visible.sync="MenudialogFormVisible" width="30%">
                    <el-tree
                            :data="menuData"
                            :props="props"
                            show-checkbox
                            node-key="id"
                            :default-expanded-keys="expends"
                            :default-checked-keys="checks"
                            @check-change="handleCheckChange">
                    </el-tree>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="MenudialogFormVisible= false">取 消</el-button>
                        <el-button type="primary" @click="Add">确 定</el-button>
                    </div>
                </el-dialog>


                <el-dialog :title="title" :visible.sync="dialogFormVisible" width="30%">
                    <el-form :model="form" >
                        <el-form-item label="名称">
                            <el-input v-model="form.name" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="描述">
                            <el-input v-model="form.description" autocomplete="off"></el-input>
                        </el-form-item>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="Add">确 定</el-button>
                    </div>
                </el-dialog>

            </div>
        </el-main>
    </div>
</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "role",
        data(){
            return {
                tableData: [],
                total:0,
                pageSize:8,
                pageNum:1,
                name:"",
                dialogFormVisible:false,
                title:'新增',
                form:{},
                multipleSelection: [],
                MenudialogFormVisible:false,
                menutitle:'分配菜单',
                menuData:[],
                props:{
                    label:'name'
                },
                expends: [1],
                checks: [4,5,6,7],
            }
        },
        created() {
            this.load()
        },
        methods:{
            handleSizeChange(pageSize){
                this.pageSize=pageSize
                this.load()
            },
            handleCurrentChange(pageNum){
                this.pageNum=pageNum
                this.load()
            },
            load(){
                request.get("http://localhost:9090/role/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&name="+this.name).then(res=>{
                    this.tableData=res.records
                    this.total=res.total
                })

            },
            reset(){
                this.name=""
                this.load()
            },
            newadd(){
                this.title="新增"
                this.dialogFormVisible=true
                this.form={}
            },
            Add(){
                request.post("http://localhost:9090/role",this.form).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
                //this.form=""
                this.dialogFormVisible=false
            },
            remove(e){
                request.delete("http://localhost:9090/role/"+e).then(res=>{
                    if(res){
                        this.$message.success("成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
            alldelete(){
                let ids=this.multipleSelection.map(v=>v.id)
                request.delete("http://localhost:9090/role/all",{data:ids}).then(res=>{
                    if(res){
                        this.$message.success("批量删除成功")
                        this.load()
                    }else
                        this.$message.error("失败")
                })
            },
            update(e){
                this.title='修改'
                this.form=e
                this.dialogFormVisible=true
            },
            handleCheckChange(data, checked, indeterminate) {
                console.log(data, checked, indeterminate);
            },
            selectMenu(e){
                this.MenudialogFormVisible=true
                this.request.get("http://localhost:9090/menu").then(res=>{
                    this.menuData=res
                })
            }

        }
    }
</script>

<style scoped>

</style>